#include <irrlicht.h>
#include "EventReceiver.h"
#include "main.h"

using namespace irr;
using namespace gui;
using namespace video;
using namespace core;

bool canselectsurf=true;

bool isDialogOpen=false;
bool bCtrlIsPressed=false;

//constructor
MyEventReceiver::MyEventReceiver(IrrlichtDevice * dev)
{
   device=dev;
   env = device->getGUIEnvironment();
   cnt=0;
}

bool MyEventReceiver::OnEvent(const SEvent& event)
//should return true if event was processed
{
   switch (event.EventType)
   {
       case EET_GUI_EVENT: return ERhandleGUI(event.GUIEvent);
       case EET_MOUSE_INPUT_EVENT: return ERhandleMouse(event.MouseInput);
       case EET_KEY_INPUT_EVENT: return ERhandleKey(event.KeyInput);
       default: return false;
   }
}

bool MyEventReceiver::ERhandleGUI(const SEvent::SGUIEvent& GUIEvent)
{

   s32 id = GUIEvent.Caller->getID();
   IGUIElement * dialog = GUIEvent.Caller;
   io::path filename;

   switch(GUIEvent.EventType)
   {
      case EGET_BUTTON_CLICKED:
         if (!isDialogOpen)
         {
            switch (id)
            {
               case (199): //quit
                  device->closeDevice();
                  break;
               case (101): //open
                  env->addFileOpenDialog(L"Please choose a file.",true,0,200);
                  camera->setInputReceiverEnabled(false);
                  canselectsurf=false;
                  isDialogOpen=true;
                  break;
               case (102): //close
                  closeCurrentScnFile();
                  break;
               case (103): //save
                  scnSaveFile();
                  break;
               default:
                  return false;
            }
         }
         break;

      case EGET_FILE_SELECTED:
         filename = ((IGUIFileOpenDialog*) dialog)->getFileName();

         switch(id)
         {
            case(200):  //select scn file
               //cast dialog as type IGUIFileOpenDialog and get filename
               loadScnFile(filename);
               isDialogOpen=false;
               break;

            case (201): //select texture
               scnRetexture(filename);
               isDialogOpen=false;
               break;

            default:
               return false;
         }
         canselectsurf=true;
         break;

      case EGET_FILE_CHOOSE_DIALOG_CANCELLED:
         isDialogOpen=false;
         canselectsurf=true;
         break;

      default:
         return false;
   }
   return true;
}

bool MyEventReceiver::ERhandleMouse(const SEvent::SMouseInput& MouseInput)
{
   switch (MouseInput.Event)
   {
      case EMIE_RMOUSE_PRESSED_DOWN: //rmouse
         //toggle free-mouse
         camera->setInputReceiverEnabled(!camera->isInputReceiverEnabled());
         break;
      case EMIE_LMOUSE_PRESSED_DOWN: //lmouse
         if (canselectsurf && !isDialogOpen)
            selectCurSurface(MouseInput.Control); //if control is pressed, append surface, else, don't
         break;
   }
   return false; //always return false because we need the mouse for other events
}

bool MyEventReceiver::ERhandleKey(const SEvent::SKeyInput& KeyInput)
{
   //check to see if control is pressed down
   //from v0.3
   /*if (KeyInput.Ctrl == KEY_CONTROL)
   {
       if (KeyInput.PressedDown) bCtrlIsPressed=true;
       else bCtrlIsPressed = false;
       return false; //return false because we use ctrl for other things
   }*/

   //only check key just pressed down events.
   if (!KeyInput.PressedDown) return false;

   switch (KeyInput.Key)
   {
      case KEY_KEY_T:
         env->addFileOpenDialog(L"Select new texture.",true,0,201);
         camera->setInputReceiverEnabled(false);
         canselectsurf=false;
         isDialogOpen=true;
         break;

      case KEY_UP:
         if (KeyInput.Shift)
            scnRetexture_UV(0,1);
         else
            return false;
         break;
      case KEY_DOWN:
         if (KeyInput.Shift)
            scnRetexture_UV(0,-1);
         else
            return false;
         break;
      case KEY_LEFT:
         if (KeyInput.Shift)
            scnRetexture_UV(-1,0);
         else
            return false;
         break;
      case KEY_RIGHT:
          if (KeyInput.Shift)
            scnRetexture_UV(1,0);
         else
            return false;
         break;

      case KEY_KEY_E:
         uvgrid_increase();
         break;
      case KEY_KEY_Q:
         uvgrid_decrease();
         break;
      case KEY_KEY_R:
         toggleUVresize();
         break;

      case KEY_KEY_Z:
         saySurfProps();
         break;

      case KEY_KEY_O:
         export2obj();
         break;

      default:
         return false;
   }
   return true;
}


